package com.leve.admin.controller;

import com.leve.admin.common.ResultUtil;
import com.leve.admin.domain.ResultBase;
import com.leve.admin.service.GhostService;
import com.xiaomi.xmpush.server.Constants;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

@RestController
@RequestMapping("/ghost")
@CrossOrigin(origins = {"*"}, maxAge = 3600L)
@Slf4j
public class GhostController {
    @Autowired
    private GhostService ghostService;

    @ApiOperation("小鬼列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码，从0开始，默认值0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "单页规模，默认值10", dataType = "int", paramType = "query")})
    @GetMapping(value = "ghosts", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase ghosts(HttpServletRequest httpServletRequest, @PageableDefault(size = 20) Pageable pageable) {
        String statusId = httpServletRequest.getParameter("statusId");
        String name = httpServletRequest.getParameter("name");
        PageRequest pageRequest = new PageRequest(pageable.getPageNumber() - 1, pageable.getPageSize());
        return ResultUtil.success(0, "小鬼列表", null, ghostService.ghosts(pageRequest, name, statusId));
    }

    @ApiOperation("系统推送")
    @RequestMapping(value = "sendMsg", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase sendMsg(HttpServletRequest httpServletRequest) {
        final int max = 10;
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            ids.add(httpServletRequest.getParameter(i + ""));
        }
        String text = httpServletRequest.getParameter("comments");
        String flag = httpServletRequest.getParameter("checkList");
        return ghostService.sendMsg(ids, text, flag);
    }

    @ApiOperation("发送短信")
    @RequestMapping(value = "smsMsg", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase smsMsg(HttpServletRequest httpServletRequest) {
        final int max = 10;
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            ids.add(httpServletRequest.getParameter(i + ""));
        }
        String text = httpServletRequest.getParameter("comments");
        return ghostService.smsMsg(ids, text);
    }


    @ApiOperation("创建用户分组")
    @RequestMapping(value = "creategroup", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase createGhostGroup(HttpServletRequest httpServletRequest) {
        String checkList = httpServletRequest.getParameter("checkList");
        String text = httpServletRequest.getParameter("comments");
        return ghostService.createGhostGroup(checkList, text);
    }


    @ApiOperation("获取用户分组")
    @RequestMapping(value = "getghostgroups", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase getGhostGroups(HttpServletRequest httpServletRequest) {
        String flag = httpServletRequest.getParameter("flag");
        return ghostService.getGhostGroups(flag);
    }

    @ApiOperation("用户分配")
    @RequestMapping(value = "distribution", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase distribution(HttpServletRequest httpServletRequest) {
        final int max = 10;
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            ids.add(httpServletRequest.getParameter(i + ""));
        }
        String statusId = httpServletRequest.getParameter("statusId");
        return ghostService.distribution(ids, statusId);
    }


    @ApiOperation("删除分组")
    @RequestMapping(value = "delgroup", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase delGroup(HttpServletRequest httpServletRequest) {
        String statusId = httpServletRequest.getParameter("0");
        return ghostService.delGroup(statusId);
    }

    @ApiOperation("常态用户组")
    @RequestMapping(value = "normal", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase normal() {
        return ghostService.normal();
    }


    @ApiOperation("常态用户组的用户详情")
    @RequestMapping(value = "normalghosts", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase normalGhosts(HttpServletRequest httpServletRequest, @PageableDefault(size = 20) Pageable pageable) {
        String name = httpServletRequest.getParameter("name");
        PageRequest pageRequest = new PageRequest(pageable.getPageNumber() - 1, pageable.getPageSize());
        return ghostService.normalGhosts(name, pageRequest);
    }


    @ApiOperation("信息推送")
    @RequestMapping(value = "msgpush", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase userPush(HttpServletRequest httpServletRequest) {
        Constants.useSandbox();
        final int max = 10;
        List<String> ids = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            ids.add(httpServletRequest.getParameter(i + ""));
        }
        String comments = httpServletRequest.getParameter("comments");
        String statusId = httpServletRequest.getParameter("statusId");
        return ghostService.userPush(ids, statusId, comments);
    }


}
